const ruiKePost = require('../../../electron/common/ruiKe.js');
const { machineIdSync } = require('node-machine-id');
const dayjs = require('dayjs');
const log = require('../../../electron/common/log');

// 瑞科必要参数
let inisoftkey = '';
let requestflag = 'jinzhuRGZN';
// 瑞科方法
// 初始化软件
const initRuiKe = async (inputWindow) => {
	// log.info(111);
	const res = await ruiKePost(
		{
			businessid: '1',
		},
		{
			requestflag: requestflag,
			maccode: machineIdSync(),
			versionname: '1.0.0',
		},
	);
	if (res.code == 0) {
		const ruiKeResData = JSON.parse(res.data);
		// log.info(ruiKeResData);
		if (ruiKeResData.requestflag != requestflag) {
			// log.info(111);
			return inputWindow.webContents.send('showMessageBox', {
				title: '错误',
				message: res.msg,
			});
		}
		inisoftkey = ruiKeResData.inisoftkey;
		inputWindow.webContents.send('closeLoading');
		if (ruiKeResData.softinfo.notice) {
			// log.info('有公告');
			inputWindow.webContents.send('showMessageBox', {
				title: '公告',
				message: ruiKeResData.softinfo.notice,
			});
		}
		if (ruiKeResData.softinfo.basedata) {
			imgUrl = ruiKeResData.softinfo.basedata;
		}
		// log.info(ruiKeResData);
	} else {
		return inputWindow.webContents.send('showMessageBox', {
			title: '错误',
			message: res.msg,
		});
	}
};
// 注册账号
const signInAccount = async (inputWindow,username, userpwd) => {
	const res = await ruiKePost(
		{
			businessid: '2',
			inisoftkey: inisoftkey,
		},
		{
			requestflag: requestflag,
			maccode: machineIdSync(),
			username,
			userpwd,
		},
	);

	if (res.code == 0) {
		const ruiKeResData = JSON.parse(res.data);
		if (ruiKeResData.requestflag != requestflag) {
			return inputWindow.webContents.send('showMessageBox', {
				title: '错误',
				message: res.msg,
			});
		}
		return inputWindow.webContents.send('showMessageBox', {
			title: '成功',
			message: `账号注册${res.msg}`,
		});
	} else {
		return inputWindow.webContents.send('showMessageBox', {
			title: '错误',
			message: res.msg,
		});
	}
};
// 登录账号
const loginInAccount = async (inputWindow,username, userpwd) => {
	const res = await ruiKePost(
		{
			businessid: '3',
			inisoftkey: inisoftkey,
		},
		{
			requestflag: requestflag,
			maccode: machineIdSync(),
			username,
			userpwd,
		},
	);
	if (res.code == 0) {
		const ruiKeResData = JSON.parse(res.data);
		if (ruiKeResData.requestflag != requestflag) {
			return inputWindow.webContents.send('showMessageBox', {
				title: '错误',
				message: res.msg,
			});
		}
		inputWindow.webContents.send('showMessageBox', {
			title: '成功',
			message: `登录${res.msg},到期时间：${ruiKeResData.endtime}`,
			router: true,
		});
		// await heartbeatFun(inputWindow,ruiKeResData.heartbeatkey,username,ruiKeResData.token);
	} else {
		return inputWindow.webContents.send('showMessageBox', {
			title: '错误',
			message: res.msg,
		});
	}
};
// 获取账号信息
const getAccountInfo = async (inputWindow,username, userpwd) => {
	const res = await ruiKePost(
		{
			businessid: '15',
			inisoftkey: inisoftkey,
		},
		{
			requestflag: requestflag,
			maccode: machineIdSync(),
			username,
			userpwd,
		},
	);
	if (res.code == 0) {
		const ruiKeResData = JSON.parse(res.data);
		if (ruiKeResData.requestflag != requestflag) {
			inputWindow.webContents.send('showMessageBox', {
				title: '错误',
				message: res.msg,
			});
			return false;
		}
		if (ruiKeResData.maccode == '') {
			return true;
		}
		let bjMaccode = machineIdSync();
		if (ruiKeResData.maccode != bjMaccode) {
			inputWindow.webContents.send('showMessageBox', {
				title: '错误',
				message: '机器码错误，请解绑使用！',
			});
			return false;
		}
		return true;
	} else {
		return inputWindow.webContents.send('showMessageBox', {
			title: '错误',
			message: res.msg,
		});
	}
};
// 修改密码
const editAccountPwd = async (inputWindow,username, userpwd, newpwd) => {
	const res = await ruiKePost(
		{
			businessid: '10',
			inisoftkey: inisoftkey,
		},
		{
			requestflag: requestflag,
			maccode: machineIdSync(),
			username,
			userpwd,
			newpwd,
		},
	);

	if (res.code == 0) {
		const ruiKeResData = JSON.parse(res.data);
		if (ruiKeResData.requestflag != requestflag) {
			return inputWindow.webContents.send('showMessageBox', {
				title: '错误',
				message: res.msg,
			});
		}
		inputWindow.webContents.send('showMessageBox', {
			title: '成功',
			message: `修改密码${res.msg}`,
		});
	} else {
		return inputWindow.webContents.send('showMessageBox', {
			title: '错误',
			message: res.msg,
		});
	}
};
// 充值\续费
const accountRenew = async (inputWindow,cardnumorusername, rechcardnum) => {
	// log.info('充值');
	const res = await ruiKePost(
		{
			businessid: '19',
			inisoftkey: inisoftkey,
		},
		{
			requestflag: requestflag,
			maccode: machineIdSync(),
			cardnumorusername,
			rechcardnum,
		},
	);

	if (res.code == 0) {
		inputWindow.webContents.send('showMessageBox', {
			title: '成功',
			message: `充值${res.msg}`,
		});
	} else {
		return inputWindow.webContents.send('showMessageBox', {
			title: '错误',
			message: res.msg,
		});
	}
};
// 解绑
const untieMacCode = async (inputWindow,cardnumorusername) => {
	const res = await ruiKePost(
		{
			businessid: '9',
			inisoftkey: inisoftkey,
		},
		{
			requestflag: requestflag,
			maccode: machineIdSync(),
			cardnumorusername,
		},
	);

	if (res.code == 0) {
		const ruiKeResData = JSON.parse(res.data);
		// log.info(ruiKeResData);
		if (ruiKeResData.requestflag != requestflag) {
			return inputWindow.webContents.send('showMessageBox', {
				title: '错误',
				message: res.msg,
			});
		}
		inputWindow.webContents.send('showMessageBox', {
			title: '成功',
			message: `解绑${res.msg}`,
		});
	} else {
		return inputWindow.webContents.send('showMessageBox', {
			title: '错误',
			message: res.msg,
		});
	}
};
// 登录心跳
const heartbeatFun = async (inputWindow,heartbeatkey,username,token) => {
	let key = heartbeatkey
	while(true){
		const res = await ruiKePost(
			{
				businessid: '5',
				inisoftkey: inisoftkey,
			},
			{
				requestflag: requestflag,
				maccode: machineIdSync(),
				heartbeatkey: key,
				timestamp:dayjs().valueOf(),
				cardnumorusername:username,
				token:token
			},
		);
		// console.log(res)
		if(res.code == 0){
			const data = JSON.parse(res.data)
			key = data.heartbeatkey
		}else{
			// console.log(res)
			return inputWindow.webContents.send('showMessageBox', {
				title: '错误',
				message: res.msg,
				type: '顶号或到期',
			});
		}
		await sleep(6 * 60 * 1000)
	}
	// const eTime = dayjs(endTime).valueOf();
	// // log.info(eTime, 'etime');
	// while (true) {
	// 	if (dayjs().valueOf() > eTime) {
	// 		return inputWindow.webContents.send('showMessageBox', {
	// 			title: '错误',
	// 			message: res.msg,
	// 			type: '顶号或到期',
	// 		});
	// 	}
	// 	await sleep(5 * 1000);
	// }
};

module.exports = {
	initRuiKe,
	signInAccount,
	loginInAccount,
	getAccountInfo,
	editAccountPwd,
	accountRenew,
	untieMacCode,
};
